package cn.bone.gateway.news.service;

import cn.bone.gateway.dao.JedisDao;
import cn.bone.gateway.news.NewsDo;
import cn.bone.gateway.news.dao.NewsDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by superbone on 2017/4/6.
 */
@Service
public class NewsService {

    @Resource
    private NewsDao newsDao;
    @Resource
    private JedisDao jedisDao;

    private Logger logger = LoggerFactory.getLogger("新闻服务");

    //每次执行完后100ms执行下一次.
    //@Scheduled(fixedDelay = 100)
    public void importNews(){

        String dcwNewsId = jedisDao.getString("dcwNewsId");
        if(StringUtils.isEmpty(dcwNewsId)){
            dcwNewsId = "0";
        }
        int newsId = Integer.parseInt(dcwNewsId);

        NewsDo oneNews = newsDao.getFirstOne(newsId);

        if(oneNews==null){
            logger.info("暂时没有新闻");
            return;
        }

        newsDao.saveNews(oneNews.getNews());
        newsDao.saveNewsDetail(oneNews.getDetail());

        dcwNewsId = String.valueOf(oneNews.getNews().getNewsId());
        jedisDao.setString("dcwNewsId",dcwNewsId);
        logger.info("导入新闻<"+dcwNewsId+">  "+oneNews.getNews().getTitle());
    }
}
